<template>
  <lay-container fluid="true" style="padding: 10px">
    <lay-row :space="10">
      <lay-col :md="24">
        <lay-card shadow="hover">
          <lay-table :page="page" :columns="columns" :dataSource="dataSource" :default-toolbar="defaultToolbar"
            v-model:selectedKeys="selectedKeys" @change="change" :size="tablesize" :default-expand-all="true">
            <template v-slot:toolbar>
              <lay-button type="primary" size="sm" @click="add" v-permission="PerKey.UserDept_AddOrEditView"
                prefix-icon="layui-icon-addition">新增
              </lay-button>
            </template>
            <template v-slot:name="{ data }">
              {{ data.name }}
            </template>
            <template v-slot:res="{ data }">
              <lay-button type="primary" size="sm" @click="contentCateRes(data.id, data.name)"
                v-permission="PerKey.UserDept_Resource" prefix-icon="layui-icon-website">网站栏目
              </lay-button>
              <lay-button type="normal" size="sm" @click="DocRes(data.id, data.name)"
                v-permission="PerKey.UserDept_Resource" prefix-icon="layui-icon-form">公文管理
              </lay-button>
            </template>
            <template v-slot:operator="{ data }">
              <lay-button-group>
                <lay-button type="primary" size="sm" @click="edit(data.id)" v-permission="PerKey.UserDept_AddOrEditView"
                  prefix-icon="layui-icon-edit">编辑</lay-button>
                <lay-button type="danger" size="sm" @click="del(data)" v-permission="PerKey.UserDept_Delete"
                  prefix-icon="layui-icon-delete">删除
                </lay-button>
              </lay-button-group>
            </template>
          </lay-table>
        </lay-card>
      </lay-col>
    </lay-row>

    <lay-layer :title="addoredittip" v-model="modelVisible" :resize="true" :area="['800px']">
      <lay-card style="padding:10px;">
        <lay-row space="10">
          <lay-form :model="form">
            <lay-col :md="24">
              <lay-form-item label="上级部门" prop="pid" required initValidate>
                <lay-col md="8">
                  <lay-select v-model="form.pid">
                    <lay-select-option v-for="cate in pids" :value="cate.id" :label="cate.title"
                      :disabled="cate.id == form.id"></lay-select-option>
                  </lay-select>
                </lay-col>
                <lay-col md="8" class="tip">不能选择自己作为上级部门</lay-col>
              </lay-form-item>
              <lay-form-item label="部门名称" prop="name" required required-error-message="部门名称不能空">
                <lay-col md="8">
                  <lay-input v-model="form.name"></lay-input>
                </lay-col>
              </lay-form-item>

              <lay-form-item label="排序" prop="orderNum">
                <lay-col md="8">
                  <lay-input-number v-model="form.orderNum"></lay-input-number>
                </lay-col>
              </lay-form-item>
              <lay-form-item label="描述" prop="remark">
                <lay-col md="14">
                  <lay-textarea placeholder="请输入描述" v-model="form.remark">
                  </lay-textarea>
                </lay-col>
              </lay-form-item>
              <lay-col :md="24" class="saveBtn">
                <lay-button @click="submitClick" type="primary" v-permission="PerKey.UserDept_AddOrEdit"
                  prefix-icon="layui-icon-ok">
                  {{ addoredittip }}
                </lay-button>
              </lay-col>
            </lay-col>
          </lay-form>
        </lay-row>
      </lay-card>
    </lay-layer>

    <lay-layer :title="titleLayer" v-model="memberResVis" :resize="true" :maxmin="true" :area="['600px', '700px']">
      <CmsCatRes :deptId="deptId" v-model:dialogIsOk="dialogIsOk"></CmsCatRes>
    </lay-layer>

    <lay-layer :title="DocLayer" v-model="DocResVis" :resize="true" :maxmin="true" :area="['600px', '700px']">
      <DocCatRes :deptId="deptId" v-model:DocIsOk="DocIsOk"></DocCatRes>
    </lay-layer>
  </lay-container>
</template>

<script lang="ts" setup>
import { ref, toRefs, reactive, watch } from "vue";
import { layer } from "@layui/layer-vue";
import { getList, delOne, getEdit, putEdit, postAdd } from "../../../api/module/user/userdept";
import { PerKey } from "../../../library/permissionConst"
import { UserDept } from '../../../types/user'
import CmsCatRes from './components/CmsCatRes.vue'
import DocCatRes from './components/DocCatRes.vue'

const selectedKeys = ref(["1"]);
const defaultToolbar = ref(true);
const page = ref({ total: 0, limit: 10, current: 1, showRefresh: true, });
const tablesize = ref('md');
const columns = [
  { title: "部门名称", width: "380px", key: "name", customSlot: "name", },
  { title: "Id", width: "40px", key: "id" },
  { title: "资源分配", customSlot: "res" },  
  { title: "排序", width: "80px", key: "orderNum" },
  { title: "备注", key: "remark", width: "280px" },
  { title: "操作", width: "200px", customSlot: "operator", key: "operator", align: "center", }
]
const dataSource = ref([])

const GetList = function () {
  getList().then((res) => {
    if (res.status == 1) {
      dataSource.value = res.data.data
    }
  })
}

const change = function () {
  GetList();
};

const edit = function (id: number) {
  changeVisible(id)
}
const add = function () {
  changeVisible(0)
}

const del = function (data: any) {
  layer.confirm("确认删除ID: " + data.id + data.name + " 部门", {
    title: "警告", move: "true", shade: "false", shadeClose: "true",
    btn: [{
      text: '确认', callback: (layerId: number) => {
        delOne(data.id).then((res) => {
          if (res.status == 1) {
            layer.msg(res.msg, { time: 1000, icon: 1 }, () => {
              GetList()
            });
          } else {
            layer.msg(res.msg);
          }
          layer.close(layerId);
        })
      }
    }, {
      text: '取消', callback: function (layerId: number) {
        layer.close(layerId);
      }
    }]
  })
}

GetList()

//添加或者编辑
const pids = ref({ targetList: [] as any, })
const modelVisible = ref(false)
const data = reactive({
  form: <UserDept><unknown>[]
})

const { form } = toRefs(data)
let addoredittip = ref();

const changeVisible = function (id: number) {
  modelVisible.value = !modelVisible.value
  if (id == 0) {
    addoredittip.value = "新增部门";
    getEdit(id).then((res) => {
      pids.value = res.data.deptList
      form.value = {
        id: 0,
        pid: 1,
        type:0,
        name: "",
        orderNum: 1,
        remark: "",
      }
    })
    return
  } else {
    addoredittip.value = "编辑部门";
    getEdit(id).then((res) => {
      pids.value = res.data.deptList
      if (res.status == 1) {
        let data = res.data.data
        form.value = data
      }
    })
    return
  }
}

const submitClick = function () {
  if (!form.value.name) {
    layer.msg("部门名称不能空")
    return
  }
  if (form.value.id == 0) {
    postAdd(form.value).then((res) => {
      done(res)
    });
  } else {
    putEdit(form.value).then((res) => {
      done(res)
    });
  }
};
const done = (res: any) => {
  if (res.status == 1) {
    layer.msg(res.msg, { time: 1000, icon: 1 }, () => { });
    modelVisible.value = !modelVisible.value
    GetList()
  } else {
    layer.msg(res.msg, { icon: 2 });
    return false;
  }
}

//网站栏目资源分配
let deptId = ref()
let titleLayer = ref();
const memberResVis = ref(false)
const dialogIsOk = ref()
const contentCateRes = function (id: number, name: string) {
  memberResVis.value = !memberResVis.value
  titleLayer.value = name + " - 分配网站栏目资源";
  deptId.value = id
  dialogIsOk.value = 0
}

watch(dialogIsOk, (val, old) => {
  if (val == 1) {
    memberResVis.value = !memberResVis.value
  }
})


//网站栏目资源分配

let DocLayer = ref();
const DocResVis = ref(false)
const DocIsOk = ref()
const DocRes = function (id: number, name: string) {
  DocResVis.value = !DocResVis.value
  DocLayer.value = name + " - 分配公文资源";
  deptId.value = id
  DocIsOk.value = 0
}

watch(DocIsOk, (val, old) => {
  if (val == 1) {
    DocResVis.value = !DocResVis.value
  }
})
</script>

<style scoped>

</style>